﻿using Chain.Wechat;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

/// <summary>
/// Red_pocket 的摘要说明 0 失败出错 1 成功 -1 超时 -2 超过提现次数 -3 商户提现上限 -4 没有openid -5 超出金额 -6 不可以提现到红包
/// </summary>
public class Red_pocket
{

    private int Get_Bool(int memid, decimal red_money, int type)
    {
        int flag = 0;
        if (red_money>200)
        {
            flag = -5;
        }
        Chain.BLL.WxRedPacketsList BllWxRedPacketsList = new Chain.BLL.WxRedPacketsList();
        DataSet ds = BllWxRedPacketsList.GetList(" memid=" + memid.ToString() + " and dateadd(Minute,1,datetime)>getdate()");
        if (ds.Tables[0].Rows.Count > 0)
        {
            flag = -1;
        }
        ds = BllWxRedPacketsList.GetList_Count(" memid=" + memid.ToString() + " and DateDiff(dd,datetime,getdate())=0  and state=1");
        int _rednum = 0;
        if (ds.Tables[0].Rows.Count>0)
        {
            _rednum = (int)ds.Tables[0].Rows[0][0];
        }
        Chain.BLL.SysParameter BllSysParameter = new Chain.BLL.SysParameter();
        ds = BllSysParameter.GetAllList();

        if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["DepositLimitTimes"] != DBNull.Value && _rednum >= (int)ds.Tables[0].Rows[0]["DepositLimitTimes"] && (int)ds.Tables[0].Rows[0]["DepositLimitTimes"]!=0)
        {
            flag = -2;
        }
        decimal summoney = BllWxRedPacketsList.GetList_summoney(" DateDiff(dd,datetime,getdate())=0  and state=1");
        decimal moenycerk = ds.Tables[0].Rows[0]["DepositLimitMoney"] == DBNull.Value ? 0 : (decimal)ds.Tables[0].Rows[0]["DepositLimitMoney"];
        if (moenycerk!=0&&summoney+red_money>moenycerk)
        {
            flag = -3;
        }
        if (type==1&&ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["IsDepositToWxRedPocket"] != DBNull.Value&&(int)ds.Tables[0].Rows[0]["IsDepositToWxRedPocket"]==0)
        {
            flag = -6;
        }
        return flag;
    
    
    
    }
    public int Redpocket(string wishing, string act_name, string remark, decimal red_money, int memid, int type, int OperatorId)
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
        int flag = 0;


        try
        {
            flag = Get_Bool(memid, red_money, type);
            Chain.BLL.Mem BllMem = new Chain.BLL.Mem();
            string openid = BllMem.getopenid(memid);
            if (openid=="")
            {
                flag = -4;
            }
            else if (flag >= 0)
            {

          

            string xml = Red_Send(red_money, wishing, act_name, remark, memid, openid);
            flag = Data_Add(xml, type, memid, red_money, openid, OperatorId);

            }
        }
        catch (Exception)
        {
            flag = 0;
            throw;
        }
        return flag;
	}
    private string Red_Send(decimal red_money, string wishing, string act_name, string remark,int Memid,string openid)
    {
        int retmoney = Convert.ToInt32(red_money * 100);
        //获得红包
        wxapi_values urlkey = new wxapi_values();
        string url_key = "CCDDD2DE691FF2D56B651877F72D90ED";
        urlkey.SetValue("url_key", url_key);
        urlkey.SetValue("mch_billno", PubFunction.curParameter.strMemPointChangePrefix + DateTime.Now.ToString("yyMMddHHmmssffff"));
        urlkey.SetValue("total_amount", retmoney);
        urlkey.SetValue("wishing", wishing);
        urlkey.SetValue("act_name", act_name);
        urlkey.SetValue("remark", remark);

       
        urlkey.SetValue("re_openid", openid);
        string parameter = urlkey.set_orderby();
        parameter = urlkey.Sign(parameter, "&as=" + url_key, "utf-8").ToUpper();
        urlkey.SetValue("parameter", parameter);
        string orderby = urlkey.set_orderby();
        HttpRequestHelper post = new HttpRequestHelper();
        wxapi_values wx_ = new wxapi_values();
        string url = wx_.GetConnStr("redPackage");
        string ret = post.Reqeust(url, orderby);
        return ret;
    }
    private int Data_Add(string xml, int type, int memid, decimal red_money, string openid, int OperatorId)
    {
        int falt;
        System.Xml.XmlDocument xx = new System.Xml.XmlDocument();
        xx.LoadXml(xml);//加载xml
        System.Xml.XmlNode item = xx.SelectSingleNode("xml");
        string return_code = item.SelectSingleNode("return_code").InnerText;
        string return_msg = item.SelectSingleNode("return_msg").InnerText;
        string result_code = item.SelectSingleNode("result_code").InnerText;
        string err_code = item.SelectSingleNode("err_code").InnerText;
        string err_code_des = item.SelectSingleNode("err_code_des").InnerText;
        string mch_billno = item.SelectSingleNode("mch_billno").InnerText;
        string mch_id = item.SelectSingleNode("mch_id").InnerText;
        string wxappid = item.SelectSingleNode("wxappid").InnerText;
        string re_openid = item.SelectSingleNode("re_openid").InnerText;
        string total_amount = item.SelectSingleNode("total_amount").InnerText;
        string send_listid = "";
        Chain.Model.WxRedPacketsList WxRedPacketsListModel = new Chain.Model.WxRedPacketsList();
        WxRedPacketsListModel.money = red_money;
        WxRedPacketsListModel.SerialNumber = mch_billno;
        WxRedPacketsListModel.datetime = DateTime.Now;
        WxRedPacketsListModel.memid = memid;
        if (err_code != null && err_code != "SUCCESS" && err_code != "")
        {
            falt = 0;
            WxRedPacketsListModel.state = 0;
        }
        else
        {
            falt = 1;
            WxRedPacketsListModel.state = 1;
            send_listid = item.SelectSingleNode("send_listid").InnerText;
        }
        WxRedPacketsListModel.type = type;
        WxRedPacketsListModel.msg = return_msg;
        
        WxRedPacketsListModel.openid = openid;
        WxRedPacketsListModel.status = err_code;
        WxRedPacketsListModel.OperatorId = OperatorId;
        WxRedPacketsListModel.send_listid = send_listid;
        Chain.BLL.WxRedPacketsList WxRedPacketsListBLL = new Chain.BLL.WxRedPacketsList();
        int ac = WxRedPacketsListBLL.Add(WxRedPacketsListModel);
        return falt;
    }
}